package address;

import org.junit.*;
import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import java.io.*;

public class AddressTest {

	String dbURL = "jdbc:mysql://localhost:3306/jmk?useUnicode=true&characterEncoding=utf-8";
	Connection conn;
	Statement stmt;

	@BeforeClass
	public static void setUpClass() throws Exception {

	}

	@AfterClass
	public static void tearDownClass() throws Exception {

	}

	// 각 메서드가 실행되기 전에 항상 호출됨
	@Before
	public void setUp() throws Exception {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("Error: com.mysql.jdbc.Driver");
		}

		try {

			conn = DriverManager.getConnection(dbURL, "root", "websys");
			stmt = conn.createStatement();
			stmt.executeUpdate("UPDATE user SET pw='0000' WHERE id='user'");

		}

		catch (SQLException ex) {
			conn = DriverManager.getConnection(dbURL, "root", "websys");
			stmt = conn.createStatement();
			System.out.println("안된당");
			conn.close();
		}
	}

	// 각 메서드의 검사가 끝나면 항상 처리되는 메소드
	@After
	public void tearDown() throws Exception {

	}

	@Test
	public void testLogin() {
		Main main = new Main();
		assertTrue("아이디와 비밀번호가 일치하지 않습니다.", main.login("0000", "user"));
		System.out.println("********Success testLogin()********");
	}

	@Test
	public void testUpdatePW() {
		PwEdit.update_pw("1111");

		try {
			stmt = conn.createStatement();
			ResultSet rs = stmt
					.executeQuery("SELECT * FROM user WHERE id='user'");
			rs.next();
			if (rs.getString("pw").equals("1111"))
				System.out.println("********Success testUpdatePW()!********");
			else
				System.out.println("Fail testUpdatePW()!");
			rs.close();
			stmt.close();
		} catch (SQLException ex) {
			System.out.print("Error: SQL In testUpdatePW()");
			ex.printStackTrace();

		}

	}

	@Test
	public void testSearchResultNum() {// 갯수가 일치하는지// 필드값이 일치하는지 //성이 일치할 경우 //
										// 이름이 일치할 경우
		SearchAddr search = new SearchAddr();

		try {
			stmt = conn.createStatement();

			stmt.execute("DELETE FROM address");
			stmt.execute("INSERT INTO address(lastName, firstName, address, mobileNumber) VALUES ('이', '기용', '분당', '01022223333')");
			stmt.execute("INSERT INTO address(lastName, firstName, address, mobileNumber) VALUES ('김', '소이', '노원', '01033334433')");

			search.search_query("이");

			if (search.getCount() == 2)
				System.out.println("********Success getCount()!********");
			else
				System.out.println("Fail getCount()!");

		} catch (SQLException ex) {
			System.out.print(ex);
			ex.printStackTrace();
		}
	}

	@Test
	public void testSearchLastName() {
		SearchAddr search = new SearchAddr();

		try {
			stmt = conn.createStatement();

			stmt.execute("DELETE FROM address");
			stmt.execute("INSERT INTO address(no, lastName, firstName, address, mobileNumber) VALUES (1, '이', '기용', '분당', '01022223333')");

			search.search_query("이");

			if (search.getNumber().equals("1"))
				System.out
						.println("********Success getSearchLastName()!********");
			else
				System.out.println("Fail getSearchLastName()!");

		} catch (SQLException ex) {
			System.out.print(ex);
			ex.printStackTrace();
		}
	}

	@Test
	public void testSearchFirstName() {
		SearchAddr search = new SearchAddr();

		try {
			stmt = conn.createStatement();

			stmt.execute("DELETE FROM address");
			stmt.execute("INSERT INTO address(no, lastName, firstName, address, mobileNumber) VALUES (2, '김', '소이', '노원', '01033334433')");

			search.search_query("이");

			if (search.getNumber().equals("2"))
				System.out
						.println("********Success getSearchFirstName()!********");
			else
				System.out.println("Fail getSearchFirstName()!");

		} catch (SQLException ex) {
			System.out.print(ex);
			ex.printStackTrace();
		}
	}

	@Test
	public void testAddrCount() {
		AddrList addrList = new AddrList();

		try {
			stmt = conn.createStatement();

			stmt.execute("DELETE FROM address");
			stmt.execute("INSERT INTO address(lastName, firstName, address, mobileNumber) VALUES ('김', '소이', '서울', '01011112222')");
			stmt.execute("INSERT INTO address(lastName, firstName, address, mobileNumber) VALUES ('이', '기용', '부산', '01022223333')");
			stmt.execute("INSERT INTO address(lastName, firstName, address, mobileNumber) VALUES ('박', '노을', '인천', '01033334444')");
			stmt.execute("INSERT INTO address(lastName, firstName, address, mobileNumber) VALUES ('고', '정인', '대구', '01044445555')");
			stmt.execute("INSERT INTO address(lastName, firstName, address, mobileNumber) VALUES ('정', '솔', '경기', '01055556666')");

			addrList.showList_query();

			if (addrList.getAddrCount().equals("5"))
				System.out.println("********Success AddrCount()!********");
			else
				System.out.println("Fail AddrCount()!");

		} catch (SQLException ex) {
			System.out.print(ex);
			ex.printStackTrace();
		}
	}

	@Test
	public void testCheckNum() {
		AddAddr addaddr = new AddAddr();
		String check = addaddr.check_num("1234");
		if (check.equals("1234")) {
			System.out.println("********Success testCheckNum()!********");
		} else

			System.out.println("Fail testCheckNum()!");
	}
	
	@Test
	public void testCheckNum2() {
		AddAddr addaddr = new AddAddr();
		String check = addaddr.check_num("숫자아님");
		if (check.equals("")) {
			System.out.println("********Success testCheckNum()!********");
		} else

			System.out.println("Fail testCheckNum()!");
	}

	@Test
	public void testNotNull() {
		AddAddr addaddr = new AddAddr();
		assertTrue("필수 입력값에 공백은 허용되지 않습니다.",
				addaddr.notnull_check("hong", "kildong", "01011112222"));
		System.out.println("********Success testNotNUll()********");
	}

	@Test
	public void testModify() {
		try {
			stmt = conn.createStatement();
			stmt.execute("DELETE FROM address WHERE no=1");
			stmt = conn.createStatement();
			stmt.execute("INSERT INTO address(no, lastName, firstName, mobileNumber) VALUES ('1', 'before', 'before', '01011112222')");

			SearchAddr search = new SearchAddr();
			search.modify_query("test", "test", "test", "test", "test", "test","1111", "test", "1111", "test", "test", 1);

			ResultSet rs = stmt
					.executeQuery("SELECT * FROM address WHERE no=1");
			rs.next();
			if (rs.getString("lastName").equals("test")
					& rs.getString("firstName").equals("test")
					& rs.getString("position").equals("test")
					& rs.getString("division").equals("test")
					& rs.getString("company").equals("test")
					& rs.getString("address").equals("test")
					& rs.getString("phoneNumber").equals("1111")
					& rs.getString("faxNumber").equals("test")
					& rs.getString("mobileNumber").equals("1111")
					& rs.getString("email").equals("test")
					& rs.getString("homepage").equals("test"))
				System.out.println("********Success testModify()!********");
			else
				System.out.println("Fail testModify()!");
			rs.close();
			stmt.close();
		} catch (SQLException ex) {
			System.out.print("Error: SQL In testModify()");
			ex.printStackTrace();

		}

	}

	@Test
	public void testDelete() {
		SearchAddr search = new SearchAddr();
		search.delete_query("1");

		try {
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT no FROM address");
			while (rs.next()) {
				if (rs.getString("no").equals("1"))
					System.out.println("Fail testDelete()!");
				else
					System.out.println("********Success testDelete()!********");

				break;
			}
			rs.close();

			stmt.close();
		} catch (SQLException ex) {
			System.out.print("Error: SQL In testDelete()");
			ex.printStackTrace();

		}

	}

	@Test
	public void testDeleteAll() {
		AddrList address = new AddrList();
		address.deleteall_query();

		try {
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM address");
			while (rs.next()) {
				if (rs.getString(1).equals("0"))
					System.out.println("********Success testDeleteAll()!********");
				else
					System.out.println("Fail testDeleteAll()!");

				break;
			}
			rs.close();

			stmt.close();
		} catch (SQLException ex) {
			System.out.print("Error: SQL In testDeleteAll()");
			ex.printStackTrace();

		}

	}

	@Test
	public void testDetailLastName() {

		try {
			SearchAddr search = new SearchAddr();
			stmt = conn.createStatement();
			stmt.execute("DELETE FROM address WHERE no=1");
			stmt = conn.createStatement();
			stmt.execute("INSERT INTO address(no, lastName, firstName, address, mobileNumber) VALUES ('1', 'test', 'Detail','강남', '01033334444')");
			search.detailInfo_query("1");
			String lastname = search.getLastName();

			stmt = conn.createStatement();
			ResultSet rs = stmt
					.executeQuery("SELECT * FROM address WHERE no=1");
			rs.next();
			if (rs.getString("lastName").equals(lastname))
				System.out.println("********Success testDetailLastName()!********");
			else
				System.out.println("Fail testDetailLastName()!");
			rs.close();
			stmt.close();
		} catch (SQLException ex) {
			System.out.print("Error: SQL In testDetailLastName()");
			ex.printStackTrace();

		}

	}

	@Test
	public void testDetailFirstName() {

		try {
			SearchAddr search = new SearchAddr();
			stmt = conn.createStatement();
			stmt.execute("DELETE FROM address WHERE no=1");
			stmt = conn.createStatement();
			stmt.execute("INSERT INTO address(no, lastName, firstName, address, mobileNumber) VALUES ('1', 'test', 'Detail', '강남','01033334444')");
			search.detailInfo_query("1");
			String firstname = search.getFirstName();

			stmt = conn.createStatement();
			ResultSet rs = stmt
					.executeQuery("SELECT * FROM address WHERE no=1");
			rs.next();
			if (rs.getString("firstName").equals(firstname))
				System.out.println("********Success testDetailFirstName()!********");
			else
				System.out.println("Fail testDetailFirstName()!");
			rs.close();
			stmt.close();
		} catch (SQLException ex) {
			System.out.print("Error: SQL In testDetailFirstName()");
			ex.printStackTrace();

		}

	}

	@Test
	public void testDetailMobileNum() {

		try {
			SearchAddr search = new SearchAddr();
			stmt = conn.createStatement();
			stmt.execute("DELETE FROM address WHERE no=1");
			stmt = conn.createStatement();
			stmt.execute("INSERT INTO address(no, lastName, firstName, address, mobileNumber) VALUES ('1', 'test', 'Detail','강남', '01033334444')");
			search.detailInfo_query("1");
			String mobilenum = search.getMobileNum();

			stmt = conn.createStatement();
			ResultSet rs = stmt
					.executeQuery("SELECT * FROM address WHERE no=1");
			rs.next();
			if (rs.getString("mobileNumber").equals(mobilenum))
				System.out.println("********Success testDetailMobileNum()!********");
			else
				System.out.println("Fail testDetailMobileNum()!");
			rs.close();
			stmt.close();
		} catch (SQLException ex) {
			System.out.print("Error: SQL In testDetailMobileNum()");
			ex.printStackTrace();

		}

	}
	
	@Test
	public void testDetailAddress() {

		try {
			SearchAddr search = new SearchAddr();
			stmt = conn.createStatement();
			stmt.execute("DELETE FROM address WHERE no=1");
			stmt = conn.createStatement();
			stmt.execute("INSERT INTO address(no, lastName, firstName, address, mobileNumber) VALUES ('1', 'test', 'Detail','강남', '01033334444')");
			search.detailInfo_query("1");
			String address = search.getAddress();

			stmt = conn.createStatement();
			ResultSet rs = stmt
					.executeQuery("SELECT * FROM address WHERE no=1");
			rs.next();
			if (rs.getString("address").equals(address))
				System.out.println("********Success testDetailAddress()!********");
			else
				System.out.println("Fail testDetailAddress()!");
			rs.close();
			stmt.close();
		} catch (SQLException ex) {
			System.out.print("Error: SQL In testDetailAddress()");
			ex.printStackTrace();

		}

	}

}
